package cn.wuyu.baselibrary.net;


import java.io.IOException;

import androidx.annotation.NonNull;
import cn.wuyu.baselibrary.net.utils.TokenUtil;
import cn.wuyu.baselibrary.utils.AppUtil;
import cn.wuyu.baselibrary.utils.DeviceUtil;
import okhttp3.HttpUrl;
import okhttp3.Interceptor;
import okhttp3.Request;
import okhttp3.Response;

/**
 * author : xuzongmeng
 * date   : 2019/2/28
 * desc   :  拦截器
 */
public class OkHttpInterceptor implements Interceptor {
    @Override
    public Response intercept(@NonNull Chain chain) throws IOException {
//        Request request = chain.request();
//        request.newBuilder()
//                .addHeader("deviceId", DeviceUtil.getIMEI(AppUtil.getAppContext()))
//                .addHeader("token", TokenUtil.getToken())
//                .build();
//        return chain.proceed(request);

//
//        Request request = chain.request();
//        if (APPConfig.DEBUG) {
//            request.newBuilder()
//                    .addHeader("deviceId", DeviceUtil.getIMEI(AppUtil.getAppContext()))
//                    .addHeader("token", TokenUtil.getToken())
//                    .build();
//        } else {
//            request.newBuilder()
//                    .addHeader("deviceId", DeviceUtil.getIMEI(AppUtil.getAppContext()))
//                    .addHeader("token", TokenUtil.getToken())
//                    .build();
//        }
//        Response response = chain.proceed(request);
////        if (!TextUtils.isEmpty(response.headers().get("server-time"))) {
////            SpUtil.putLong("server—time", Long.parseLong(response.headers().get("server-time")));
////        }
        Request original = chain.request();
        HttpUrl originalHttpUrl = original.url();
        HttpUrl.Builder builder = originalHttpUrl.newBuilder();
//        String currentUserInfo = SpUtil.getString("user_info", "");

//        String token = "XO6y6TJ8yP8f15tE07r5uGOYj4VlBn8hB2EDlf";
//        if(!TextUtils.isEmpty(currentUserInfo)){
//            try {
//                JSONObject jsonObject = new JSONObject(currentUserInfo);
//                ACCESS_TOKEN = jsonObject.getString("accessToken");
//            } catch (JSONException e) {
//                e.printStackTrace();
//            }
//        }
        HttpUrl url = builder.build();
        Request request = original
                .newBuilder()
                .addHeader("token", TokenUtil.getToken())
                .addHeader("deviceCode", DeviceUtil.getIMEI(AppUtil.getAppContext()))
                .addHeader("accept-Language", "zh")
                .url(url)
                .build();
        Response response = chain.proceed(request);
        return response;
    }
}
